package sg4c.protocol;


import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.GZIPOutputStream;
import sg4c.frontend.command.GraphicsCommand;

public class CaptureInput implements DataInputProtocol {

    private final DataInputProtocol proto;
    private final ObjectOutputStream captureStream;
    private boolean disabled = false;

    public CaptureInput(DataInputProtocol proto, OutputStream captureStream) throws IOException {
        this.proto = proto;
        this.captureStream = new ObjectOutputStream(captureStream);
        //this.captureStream = new DataOutputStream(new GZIPOutputStream(captureStream));
    }

    public void disable() {
        disabled = true;
    }

    public void enable() {
        disabled = false;
    }

    public int readInt() throws IOException {
        int val = proto.readInt();
        if (!disabled) {
            captureStream.writeInt(val);
        }
        return val;
    }

    public float readFloat() throws IOException {
        float val = proto.readFloat();
        if (!disabled) {
            captureStream.writeFloat(val);
        }
        return val;
    }

    public double readDouble() throws IOException {
        double val = proto.readDouble();
        if (!disabled) {
            captureStream.writeDouble(val);
        }
        return val;
    }

    public String readString() throws IOException {
        String val = proto.readString();
        if (!disabled) {
            captureStream.writeBytes(val);
        }
        return val;
    }

    public void close() throws IOException {
        captureStream.close();
    }

    public void writeObject(Object cmd) throws IOException {
        captureStream.writeObject(cmd);
    }

    public void flush() throws IOException{
        captureStream.flush();
        captureStream.reset();
    }

    public void next() throws IOException {
        proto.next();
    }
}
